package com.vvlive.caibao.smartcity.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;

import com.vvlive.caibao.smartcity.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/8/20.
 */
public class MeetingFragment extends BaseFragment {

    private static final int ALL_MEETINGS_INDEX = 0;
    private static final int MY_MEETINGS_INDEX = 1;

    private RadioButton mAllMeetingsTab;
    private RadioButton mMyMeetingsTab;
    private ViewPager mViewPager;

    private TabClickListener mClick;
    private MeetingsPagerAdapter mAdapter;
    private List<Fragment> mFragments;

    @Override
    public int getContentRes() {
        return R.layout.fragment_meeting;
    }

    @Override
    public void initFragment() {
        mClick = new TabClickListener();
        mAllMeetingsTab = super.findView(R.id.tab_all_mettings);
        mMyMeetingsTab = super.findView(R.id.tab_my_meetings);
        mViewPager = super.findView(R.id.meetings_pager);

        mAllMeetingsTab.setOnClickListener(mClick);
        mMyMeetingsTab.setOnClickListener(mClick);
        mFragments = new ArrayList<>();
        mFragments.add(new PagerAllMeetingsFragment());
        mFragments.add(new PersonalMeetingsFragment());
        mAdapter = new MeetingsPagerAdapter();
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case ALL_MEETINGS_INDEX:
                        mAllMeetingsTab.setChecked(true);
                        break;
                    case MY_MEETINGS_INDEX:
                        mMyMeetingsTab.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    private final class TabClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.tab_all_mettings:
                    mViewPager.setCurrentItem(ALL_MEETINGS_INDEX, true);
                    break;
                case R.id.tab_my_meetings:
                    mViewPager.setCurrentItem(MY_MEETINGS_INDEX, true);
                    break;
            }
        }
    }

    private final class MeetingsPagerAdapter extends FragmentStatePagerAdapter {

        public MeetingsPagerAdapter() {
            super(getActivity().getSupportFragmentManager());
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments == null ? 0 : mFragments.size();
        }
    }

}
